package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping(value = "/member")
public class MemberController {

    @Reference
    MemberService memberService;



    @RequestMapping(value = "/getMemberSex")
    public Result getMemberSex(){
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            // 男女占比数量
            List<Map<String,Object>> memberCount = memberService.findMemberCount();
            // 名称
            List<String> memberSex = new ArrayList<>();

            for (Map<String, Object> stringObjectMap : memberCount) {
                String name = (String)stringObjectMap.get("name"); // 名称
                memberSex.add(name);
            }

            map.put("memberSex",memberSex);  // 名称
            map.put("memberCount",memberCount);  // 数量

            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS);
        }
    }

    @RequestMapping(value = "/getMemberAge")
    public Result getMemberAge(){
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            // 男女占比数量
            List<Map<String,Object>> memberAgeCount = memberService.findMemberAgeCount();
            // 名称
            List<String> memberAge = new ArrayList<>();

            for (Map<String, Object> stringObjectMap : memberAgeCount) {
                String name = (String)stringObjectMap.get("name"); // 名称
                memberAge.add(name);
            }

            map.put("memberAge",memberAge);  // 名称
            map.put("memberAgeCount",memberAgeCount);  // 数量

            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


}
